package com.byron.pss.web.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.byron.pss.dao.ILostFoundDAO;
import com.byron.pss.dao.IMessageDAO;
import com.byron.pss.dao.IUserDAO;
import com.byron.pss.dao.impl.LostFoundDAOImpl;
import com.byron.pss.dao.impl.MessageDAOImpl;
import com.byron.pss.dao.impl.UserDAOImpl;
import com.byron.pss.domain.LostFound;
import com.byron.pss.domain.LostFoundStr;
import com.byron.pss.domain.Message;
import com.byron.pss.util.StringUtils;
import com.byron.pss.util.Transform;

/**
 * 处理商品的Servlet
 * @author Byron
 *
 */
@WebServlet("/lostfound")
public class LostFoundServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	// 用户操作dao
	private IUserDAO userDao = null;
	// 留言信息操作dao
	private IMessageDAO msgDao = null;
	// 遗失物品操做dao
	private ILostFoundDAO dao = null;

	@Override
	public void init() throws ServletException {
		userDao = new UserDAOImpl();
		msgDao = new MessageDAOImpl();
		dao = new LostFoundDAOImpl();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String cmd = req.getParameter("cmd");
		if ("add".equals(cmd)) {
			// 分发到添加处理方法
			add(req, resp);
		} else if ("delete".equals(cmd)) {
			// 分发到删除处理方法
			delete(req, resp);
		} else if ("message".equals(cmd)) {
			// 查看留言信息
			message(req, resp);
		} else if ("quit".equals(cmd)) {
			// 退出登录
			quit(req, resp);
		} else {
			// 分发到显示主界面处理方法
			list(req, resp);
		}
	}

	/**
	 *	查看留言信息
	 * @throws IOException 
	 * @throws ServletException 
	 */
	protected void message(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<Message> list = msgDao.list();
		req.setAttribute("list", list);
		req.getRequestDispatcher("/WEB-INF/views/showMsg.jsp").forward(req,
				resp);
	}

	@SuppressWarnings("deprecation")
	protected void add(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String flag = req.getParameter("flag");
		if (StringUtils.hasLength(flag)) {
			// 是添加完成后过来的
			String name = req.getParameter("name");
			String describe = req.getParameter("describe");
			String location = req.getParameter("location");
			String lostTime = req.getParameter("lostTime");
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			String number = req.getParameter("number");
			String other = req.getParameter("other");
			if(!StringUtils.hasLength(name) || !StringUtils.hasLength(number) ||
					!StringUtils.hasLength(lostTime)){
				//姓名和数量只要有一个没有值，就直接回到添加页面
				req.getRequestDispatcher("/WEB-INF/views/add.jsp").forward(req,
						resp);
				return;
			}
			Date date = null;
			try {
				date = sf.parse(lostTime);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			LostFound lf = new LostFound(null, name, describe, location, date,
					Integer.valueOf(number), other);
			dao.save(lf);
			// 重定向到主界面:这里必须带上cmd,因为是请求转发，所以原来的cmd信息还存储的请求信息中，必须进行覆盖
			req.getRequestDispatcher("/lostfound?cmd=list").forward(req, resp);
		} else {
			// flag没有值，说明是遗失物品展示界面传过来的，直接到添加界面
			req.getRequestDispatcher("/WEB-INF/views/add.jsp").forward(req,
					resp);
		}
	}

	protected void delete(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String flag = req.getParameter("flag");
		// 拿到页面传过来的id值,调用dao删除
		String id = req.getParameter("id");
		if (StringUtils.hasLength(flag)) {
			// 来自展示留言信息展示界面的删除
			msgDao.delete(Long.valueOf(id));
			// 重定向回到留言展示界面
			req.getRequestDispatcher("/lostfound?cmd=message").forward(req,
					resp);
		} else {
			// 来自物品信息展示界面的删除
			dao.delete(Long.valueOf(id));
			// 重定向到主界面
			req.getRequestDispatcher("/lostfound?cmd=list").forward(req, resp);
		}
	}

	protected void list(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 封装要展示的信息
		List<LostFoundStr> list = Transform.list0jsp(dao.list());
		req.setAttribute("list", list);
		// 请求转发，传递数据到显示jsp
		req.getRequestDispatcher("/WEB-INF/views/lostfound_list.jsp").forward(
				req, resp);
	}

	protected void quit(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		String flag = req.getParameter("cmd");
		if ("quit".equals(flag)) {
			// 说明点击的是重新登录，销毁session,--直接跳转到登陆界面
			// 点击退出
			session.invalidate();
			resp.sendRedirect(req.getContextPath() + "/unchecked/login.jsp");
			return;
		}
		req.getRequestDispatcher("/index").forward(req, resp);
	}
}
